package me.j360.springboot.simple.bean.search.rule;

import me.j360.springboot.simple.bean.BaseEnum;
import org.springframework.util.Assert;

/**
 * 说明：
 * Bean类 - 查询条件（没有参数）
 *
 * ============================================================================
 * 版权所有 2015 qiushihua。
 *
 * @author qiushihua
 * @version 0.1 2015-07-22
 * ============================================================================
 */
public class RuleNull extends Rule {

    private final String field;

    private RuleNull(BaseEnum.RuleType ruleType, String field) {
        super(ruleType);
        this.field = field;
    }

    public String getField() {
        return field;
    }

    private static Builder newBuilder() {
        return new Builder();
    }

    public static RuleNull isEmpty(String field) {
        return newBuilder().setRuleType(BaseEnum.RuleType.isEmpty).setField(field).build();
    }

    public static RuleNull isNotEmpty(String field) {
        return newBuilder().setRuleType(BaseEnum.RuleType.isNotEmpty).setField(field).build();
    }

    public static RuleNull isNull(String field) {
        return newBuilder().setRuleType(BaseEnum.RuleType.isNull).setField(field).build();
    }

    public static RuleNull isNotNull(String field) {
        return newBuilder().setRuleType(BaseEnum.RuleType.isNotNull).setField(field).build();
    }

    public static class Builder extends Rule.Builder<Rule, Builder> {

        private String field;

        @Override
        protected Builder getThis() {
            return this;
        }

        public Builder setRuleType(BaseEnum.RuleType ruleType) {
            this.ruleType = ruleType;
            return this;
        }

        public Builder setField(String field) {
            this.field = field;
            return this;
        }

        @Override
        public RuleNull build() {
            Assert.notNull(ruleType, "ruleType 不能为空");
            Assert.hasText(field, "field 不能为空");
            return new RuleNull(ruleType, field);
        }

    }

}
